package org.saxion.aktiehuis.DAO;

import java.util.List;
import org.hibernate.Session;
import org.saxion.aktiehuis.DAO.interfaces.BestellingDAO;
import org.saxion.aktiehuis.model.Bestelling;
import org.saxion.aktiehuis.model.Klant;
import org.saxion.aktiehuis.model.Product;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BestellingMock extends HibernateDaoSupport implements BestellingDAO{
	
//	private SessionFactory sessionFactory;	
//
//    public void setSessionFactory(SessionFactory sessionFactory) {
//        this.sessionFactory = sessionFactory;
//    }

	public int nieuweBestelling(int klantId) throws Exception {
		Bestelling bestelling = new Bestelling();
		Session session = getSession(true);
		Klant klantdb = (Klant) session.get(Klant.class, klantId);
		bestelling.setKlant(klantdb);
		session.save(bestelling);
		//sessionFactory.getCurrentSession().flush();

		return bestelling.getId();
	}

	public void addProduct(int bestellingID, int productId, int aantal) throws Exception {
		Session session = getSession(true);
		Bestelling bestellingdb = (Bestelling) session.get(Bestelling.class, bestellingID);
		Product productdb = (Product) session.get(Product.class, productId);
		bestellingdb.addProduct(productdb, aantal);
		session.update(bestellingdb);
		//sessionFactory.getCurrentSession().flush();
		session.flush();
	}
	

	public Bestelling getBestelling(int bestellingID) throws Exception {
		Session session = getSession(true);
		Bestelling bestellingdb = (Bestelling) session.get(Bestelling.class, bestellingID);
		return bestellingdb;
	}

	public List<Bestelling> getBestellingKlant(int klantId) throws Exception {
		Session session = getSession(true);
		List<Bestelling> bestellingen = session.createQuery("FROM Bestelling b WHERE b.klant.id = :id").setParameter("id", klantId).list(); 

		return bestellingen;
	}

	public void bevestigBestelling(int bestellingId) throws Exception {
		// TODO Auto-generated method stub
		Session session = getSession(true);
		Bestelling bestellingdb = (Bestelling) session.get(Bestelling.class, bestellingId);
		bestellingdb.setBevestigd(true);
		
		session.update(bestellingdb);
		session.flush();
	}



}
